package com.cskaoyan.shiro;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

//@Component
public class CustomSessionManager extends DefaultWebSessionManager {

    private static final String TOKEN = "X-CskaoyanMarket-Admin-Token";
    private static final String WxTOKEN = "X-CskaoyanMarket-Token";

    /**
     * 执行认证 ，认证成功会返回一个token，里面的值就是sessionId
     *  前台后台的header不一样 TOKEN 和 WxTOKEN
     * @param srequest
     * @param response
     * @return
     */
    @Override
    protected Serializable getSessionId(ServletRequest srequest, ServletResponse response) {
        HttpServletRequest request = (HttpServletRequest) srequest;
        String sessionId = request.getHeader(TOKEN);
        String wxheader = request.getHeader(WxTOKEN);
        if (sessionId != null && !"".equals(sessionId)) {
            return sessionId;
        }
        if (wxheader != null && !"".equals(sessionId)) {
            return wxheader;
        }

        return super.getSessionId(request, response);
    }
}
